package com.lzy.imagepicker.ui;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.media.MediaMetadataRetriever;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextPaint;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.TextView;

import com.angcyo.uiview.recycler.RExItemDecoration;
import com.lzy.imagepicker.ImageDataSource;
import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.R;
import com.lzy.imagepicker.adapter.ImageFolderAdapter;
import com.lzy.imagepicker.adapter.ImageGridAdapter2;
import com.lzy.imagepicker.bean.ImageFolder;
import com.lzy.imagepicker.bean.ImageItem;
import com.lzy.imagepicker.view.FolderPopUpWindow;

import java.io.File;
import java.util.List;

/**
 * ================================================
 * 作    者：jeasonlzy（廖子尧 Github地址：https://github.com/jeasonlzy/ImagePicker
 * 版    本：1.0
 * 创建日期：2016/5/19
 * 描    述：
 * 修订历史：
 * ================================================
 */
public class ImageGridActivity extends ImageBaseActivity implements ImageDataSource.OnImagesLoadedListener,
        ImageGridAdapter2.OnImageItemClickListener, ImagePicker.OnImageSelectedListener, View.OnClickListener {

    public static final int REQUEST_PERMISSION_STORAGE = 0x01;
    public static final int REQUEST_PERMISSION_CAMERA = 0x02;

    /**
     * 清空之前的选择
     */
    public static final String CLEAR_SELECTOR = "clear_selector";
    /**
     * 允许选择视频的最大时长, 毫秒
     */
    public static final String MAX_VIDEO_DURATION = "max_video_duration";

    private ImagePicker imagePicker;

    private boolean isOrigin = false;  //是否选中原图
    // private GridView mGridView;  //图片展示控件
    private RecyclerView mRecyclerView;  //图片展示控件
    private View mFooterBar;     //底部栏
    private View mProgressBar;     //加载提示
    private Button mBtnOk;       //确定按钮
    private Button mBtnDir;      //文件夹切换按钮
    private Button mBtnPre;      //预览按钮
    private ImageFolderAdapter mImageFolderAdapter;    //图片文件夹的适配器
    private FolderPopUpWindow mFolderPopupWindow;  //ImageSet的PopupWindow
    private List<ImageFolder> mImageFolders;   //所有的图片文件夹
    //private ImageGridAdapter mImageGridAdapter;  //图片九宫格展示的适配器
    private ImageGridAdapter2 mImageGridAdapter;  //图片九宫格展示的适配器

    private long maxVideoDuration = -1;

    @ImageDataSource.LoaderType
    private int loadType;

    /**
     * 从视频文件中读取视频长度
     */
    public static long getVideoDuration(Context context, String videoFile) {
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        //use one of overloaded setDataSource() functions to set your data source
        String time;
        try {
            retriever.setDataSource(context, Uri.fromFile(new File(videoFile)));
            time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
            int width = Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT));
            int height = Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH));
        } finally {
            try {
                retriever.release();
            } catch (Exception ex) {
                // Ignore failures while cleaning up.
            }
        }

        long timeInMillisec = Long.parseLong(time);

        return timeInMillisec;
    }

    @Override
    protected void onPostCreate(@Nullable Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        final boolean clear = getIntent().getBooleanExtra(CLEAR_SELECTOR, true);
        maxVideoDuration = getIntent().getLongExtra(MAX_VIDEO_DURATION, -1);

        imagePicker = ImagePicker.getInstance();
        loadType = imagePicker.getLoadType();

        imagePicker.clear();
        if (clear) {
            imagePicker.clearSelectedImages();
        }

        imagePicker.addOnImageSelectedListener(this);

        findViewById(R.id.btn_back).setOnClickListener(this);

        mBtnOk = (Button) findViewById(R.id.btn_ok);
        mBtnOk.setOnClickListener(this);
        mBtnDir = (Button) findViewById(R.id.btn_dir);
        mBtnDir.setOnClickListener(this);
        mBtnPre = (Button) findViewById(R.id.btn_preview);
        mBtnPre.setOnClickListener(this);
//        mGridView = (GridView) findViewById(R.id.gridview);
        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        mRecyclerView.setLayoutManager(new GridLayoutManager(this, 3));
        mRecyclerView.addItemDecoration(new RExItemDecoration(new RExItemDecoration.ItemDecorationCallback() {

            @Override
            public Rect getItemOffsets(RecyclerView.LayoutManager layoutManager, int position, int edge) {
                Rect rect = new Rect(0, 0, 0, 0);
                int offset = (int) (getResources().getDisplayMetrics().density * 2);
                if (position % 3 != 2) {
                    rect.right = offset;
                    rect.bottom = offset;
                } else {
                    rect.bottom = offset;
                }
                return rect;
            }

            @Override
            public void draw(Canvas canvas, TextPaint paint, View itemView, Rect offsetRect, int itemCount, int position) {

            }
        }));
        mFooterBar = findViewById(R.id.footer_bar);
        if (imagePicker.isMultiMode()) {
            mBtnOk.setVisibility(View.VISIBLE);
            mBtnPre.setVisibility(View.VISIBLE);
        } else {
            mBtnOk.setVisibility(View.GONE);
            mBtnPre.setVisibility(View.GONE);
        }

        mImageGridAdapter = new ImageGridAdapter2(this, null, loadType);
        mImageGridAdapter.setMaxVideoDuration(maxVideoDuration);

        mImageFolderAdapter = new ImageFolderAdapter(this, null, loadType);

        onImageSelected(0, null, false);

        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {
            if (checkPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                startLoadMedia();
            } else {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_PERMISSION_STORAGE);
            }
        }

        TextView tv_des = (TextView) findViewById(R.id.tv_des);
        TextView btn_dir = (TextView) findViewById(R.id.btn_dir);
        if (loadType == ImageDataSource.VIDEO) {
            tv_des.setText("选择视频");
            btn_dir.setText("全部视频");
        } else if (loadType == ImageDataSource.IMAGE) {
            tv_des.setText("选择图片");
            btn_dir.setText("全部图片");
        } else if (loadType == ImageDataSource.IMAGE_AND_VIDEO) {
            tv_des.setText("图片和视频");
            btn_dir.setText("图片和视频");
        } else {
            tv_des.setText("媒体文件");
            btn_dir.setText("媒体文件");
        }
    }

    /**
     * 开始扫描媒体文件
     */
    private void startLoadMedia() {
        getWindow().getDecorView().post(new Runnable() {
            @Override
            public void run() {
                new ImageDataSource(ImageGridActivity.this, loadType, null, ImageGridActivity.this);
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_grid);
        mProgressBar = findViewById(R.id.progressBar);
        mProgressBar.setVisibility(View.VISIBLE);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (permissions.length < 1 || grantResults.length < 1) {
            return;
        }

        if (requestCode == REQUEST_PERMISSION_STORAGE) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                startLoadMedia();
            } else {
                showToast("权限被禁止，无法选择本地图片");
            }
        } else if (requestCode == REQUEST_PERMISSION_CAMERA) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                imagePicker.takePicture(this, ImagePicker.REQUEST_CODE_TAKE);
            } else {
                showToast("权限被禁止，无法打开相机");
            }
        }
    }

    @Override
    protected void onDestroy() {
        imagePicker.removeOnImageSelectedListener(this);
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.btn_ok) {
            Intent intent = new Intent();
            intent.putExtra(ImagePicker.EXTRA_RESULT_ITEMS, imagePicker.getSelectedImages());
            setResult(ImagePicker.RESULT_CODE_ITEMS, intent);  //多选不允许裁剪裁剪，返回数据
            finish();
        } else if (id == R.id.btn_dir) {
            if (mImageFolders == null) {
                Log.i("ImageGridActivity", "您的手机没有图片");
                return;
            }
            //点击文件夹按钮
            createPopupFolderList();
            mImageFolderAdapter.refreshData(mImageFolders);  //刷新数据
            if (mFolderPopupWindow.isShowing()) {
                mFolderPopupWindow.dismiss();
            } else {
                mFolderPopupWindow.showAtLocation(mFooterBar, Gravity.NO_GRAVITY, 0, 0);
                //默认选择当前选择的上一个，当目录很多时，直接定位到已选中的条目
                int index = mImageFolderAdapter.getSelectIndex();
                index = index == 0 ? index : index - 1;
                mFolderPopupWindow.setSelection(index);
            }
        } else if (id == R.id.btn_preview) {
            ImagePreviewActivity.launcher(this, imagePicker.getSelectedImages(), 0, isOrigin, loadType, maxVideoDuration);
        } else if (id == R.id.btn_back) {
            //点击返回按钮
            finish();
        }
    }

    /**
     * 创建弹出的ListView
     */
    private void createPopupFolderList() {
        mFolderPopupWindow = new FolderPopUpWindow(this, mImageFolderAdapter);
        mFolderPopupWindow.setOnItemClickListener(new FolderPopUpWindow.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                mImageFolderAdapter.setSelectIndex(position);
                imagePicker.setCurrentImageFolderPosition(position);
                mFolderPopupWindow.dismiss();
                ImageFolder imageFolder = (ImageFolder) adapterView.getAdapter().getItem(position);
                if (null != imageFolder) {
                    mImageGridAdapter.refreshData(imageFolder.images);
                    mBtnDir.setText(imageFolder.name);
                }
//                mGridView.smoothScrollToPosition(0);//滑动到顶部
                mRecyclerView.smoothScrollToPosition(0);//滑动到顶部
            }
        });
        mFolderPopupWindow.setMargin(mFooterBar.getHeight());
    }

    @Override
    public void onImagesLoaded(List<ImageFolder> imageFolders) {
        this.mImageFolders = imageFolders;
        imagePicker.setImageFolders(imageFolders);

        if (imageFolders.size() == 0) mImageGridAdapter.refreshData(null);
        else mImageGridAdapter.refreshData(imageFolders.get(0).images);
        mImageGridAdapter.setOnImageItemClickListener(this);
//        mGridView.setAdapter(mImageGridAdapter);
        mRecyclerView.setAdapter(mImageGridAdapter);
        mImageFolderAdapter.refreshData(imageFolders);

        //mProgressBar.stop();
        mProgressBar.setVisibility(View.GONE);

        if (imageFolders.isEmpty() && !imagePicker.isShowCamera()) {
            findViewById(R.id.empty_view).setVisibility(View.VISIBLE);
        } else {
            findViewById(R.id.empty_view).setVisibility(View.GONE);
        }
    }

    @Override
    public void onImageItemClick(View view, ImageItem imageItem, int position) {
        //根据是否有相机按钮确定位置
        position = imagePicker.isShowCamera() ? position - 1 : position;
        if (imagePicker.isMultiMode()) {
            //如果是多选，点击图片进入预览界面
            ImagePreviewActivity.launcher(this, imagePicker.getCurrentImageFolderItems(), position, isOrigin, loadType, maxVideoDuration);
        } else {

            if (VideoDurationControl.isVideoDurationLong(maxVideoDuration, imageItem.videoDuration,
                    loadType, imageItem.loadType)) {
                return;
            }

            imagePicker.clearSelectedImages();
            imagePicker.addSelectedImageItem(position, imagePicker.getCurrentImageFolderItems().get(position), true);
            if (imagePicker.isCrop()) {
                Intent intent = new Intent(ImageGridActivity.this, ImageCropActivity.class);
                startActivityForResult(intent, ImagePicker.REQUEST_CODE_CROP);  //单选需要裁剪，进入裁剪界面
            } else {
                Intent intent = new Intent();
                intent.putExtra(ImagePicker.EXTRA_RESULT_ITEMS, imagePicker.getSelectedImages());
                setResult(ImagePicker.RESULT_CODE_ITEMS, intent);   //单选不需要裁剪，返回数据
                finish();
            }
        }
    }

    @Override
    public void onImageSelected(int position, ImageItem item, boolean isAdd) {
        if (imagePicker.getSelectImageCount() > 0) {
            mBtnOk.setText(getString(R.string.select_complete,
                    imagePicker.getSelectImageCount() + "", imagePicker.getSelectLimit() + ""));
            mBtnOk.setEnabled(true);
            mBtnPre.setEnabled(true);
        } else {
            mBtnOk.setText(getString(R.string.complete));
            mBtnOk.setEnabled(false);
            mBtnPre.setEnabled(false);
        }
        mBtnPre.setText(getResources().getString(R.string.preview_count, imagePicker.getSelectImageCount() + ""));
        //mImageGridAdapter.notifyDataSetChanged();
        mImageGridAdapter.notifyItemChanged(position);
//        mImageGridAdapter.localRefresh(mRecyclerView, position);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null) {
            if (resultCode == ImagePicker.RESULT_CODE_BACK) {
                isOrigin = data.getBooleanExtra(ImagePreviewActivity.ISORIGIN, false);
                mImageGridAdapter.notifyDataSetChanged();
            } else if (resultCode == RESULT_OK && requestCode == ImagePicker.REQUEST_CODE_TAKE) {
                cropImage();
            } else if (resultCode == RESULT_OK && requestCode == ImagePicker.REQUEST_CODE_RECORD_VIDEO) {
                //cropImage();
                //视频返回

                //L.e("call: onActivityResult([requestCode, resultCode, data])-> 视频录制返回:" + imagePicker.getTakeImageFile().getAbsolutePath());
                ImagePicker.galleryAddPic(this, imagePicker.getTakeImageFile());
                imagePicker.clearSelectedImages();
                ImageItem imageItem = new ImageItem(ImageDataSource.VIDEO);
                imageItem.path = imagePicker.getTakeImageFile().getAbsolutePath();

                MediaMetadataRetriever retriever = new MediaMetadataRetriever();
                String time = "0";
                int width = 0;
                int height = 0;
                try {
                    retriever.setDataSource(this, Uri.fromFile(new File(imageItem.path)));
                    time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
                    height = Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT));
                    width = Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH));
                } finally {
                    try {
                        retriever.release();
                    } catch (Exception ex) {
                        // Ignore failures while cleaning up.
                    }
                }

                long timeInMillisec = Long.parseLong(time);

                imageItem.videoDuration = timeInMillisec;
                imageItem.width = width;
                imageItem.height = height;
                imageItem.resolution = width + "x" + height;

                imagePicker.getSelectedImages().add(imageItem);

                Intent intent = new Intent();
                intent.putExtra(ImagePicker.EXTRA_RESULT_ITEMS, imagePicker.getSelectedImages());
                setResult(ImagePicker.RESULT_CODE_ITEMS, intent);
                finish();
            } else {
                //从拍照界面返回
                //点击 X , 没有选择照片
                if (data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS) == null) {
                    //什么都不做
                } else {
                    //说明是从裁剪页面过来的数据，直接返回就可以
                    setResult(ImagePicker.RESULT_CODE_ITEMS, data);
                    finish();
                }
            }
        } else {
            //如果是裁剪，因为裁剪指定了存储的Uri，所以返回的data一定为null
            if (resultCode == RESULT_OK && requestCode == ImagePicker.REQUEST_CODE_TAKE) {
                cropImage();
            }
        }
    }

    private void cropImage() {
        //发送广播通知图片增加了
        ImagePicker.galleryAddPic(this, imagePicker.getTakeImageFile());
        ImageItem imageItem = new ImageItem();
        imageItem.path = imagePicker.getTakeImageFile().getAbsolutePath();
        if (imagePicker.isMultiMode()) {
        } else {
            imagePicker.clearSelectedImages();
        }
        imagePicker.addSelectedImageItem(0, imageItem, true);
        if (imagePicker.isCrop()) {
            Intent intent = new Intent(ImageGridActivity.this, ImageCropActivity.class);
            startActivityForResult(intent, ImagePicker.REQUEST_CODE_CROP);  //单选需要裁剪，进入裁剪界面
        } else {
            Intent intent = new Intent();
            intent.putExtra(ImagePicker.EXTRA_RESULT_ITEMS, imagePicker.getSelectedImages());
            setResult(ImagePicker.RESULT_CODE_ITEMS, intent);   //单选不需要裁剪，返回数据
            finish();
        }
    }
}
